﻿using ConfExpert.Forms.Base;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevTeam;
using System;
using System.Data;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo
{
    public partial class AksesuarRezerveOlustur : XForm
    {
        string Aksesuar,KaufIdx;
        int rezerveId,brandId,sezonId;
        public AksesuarRezerveOlustur(string aksesuar, string kaufIdx, int brandId = 0, int sezonId = 0)
        {
            InitializeComponent();
            Aksesuar = aksesuar;
            KaufIdx = kaufIdx;
            this.brandId = brandId;
            this.sezonId = sezonId;
        }

        public int RezerveId
        {
            get
            {
                return rezerveId;
            }
        }
        public int BrandId
        {
            get{return brandId;}
        }
        public int SezonId
        {
            get { return sezonId; }
        }
        private void AksesuarRezerveOlustur_Load(object sender, EventArgs e)
        {
            
            GetDetails("SODetay", "GetOrderRef");

            gleOrderRef.Edit.Assign(GetIntellisensedRI(ds.SODetay));
            ((RepositoryItemGridLookUpEdit)gleOrderRef.Edit).ProcessNewValue += new ProcessNewValueEventHandler(ri_ProcessNewValue2);
            ((RepositoryItemGridLookUpEdit)gleOrderRef.Edit).View.Columns["BrandId"].ColumnEdit = RI(Rp.Brand);
            ((RepositoryItemGridLookUpEdit)gleOrderRef.Edit).View.Columns["SezonId"].ColumnEdit = RI(Rp.Sezon);
            gleOrderRef.EditValueChanged += gleOrderRef_EditValueChanged;

            ExpireDate.Value = DateTime.Now.AddDays(14);
            gleBrand.Properties.Assign(RI(Rp.Brand));
            gleSezon.Properties.Assign(RI(Rp.Sezon));
            gleBrand.EditValue = brandId;
            gleSezon.EditValue = sezonId;

            chkIdle.CheckedChanged += chkIdle_CheckedChanged;
        }

        void chkIdle_CheckedChanged(object sender, EventArgs e)
        {
            gleOrderRef.Enabled = !chkIdle.Checked;
            if (chkIdle.Checked)
                gleOrderRef.EditValue = null;
        }

        void gleOrderRef_EditValueChanged(object sender, EventArgs e)
        {
            if (((RepositoryItemGridLookUpEdit)gleOrderRef.Edit).View.GetFocusedRow() != null)
            {
                DataRow dr = ((DataRowView)((RepositoryItemGridLookUpEdit)gleOrderRef.Edit).View.GetFocusedRow()).Row;
                gleBrand.EditValue = intParse(dr["BrandId"]);
                gleSezon.EditValue = intParse(dr["SezonId"]);
            }
        }

       

        private void btnTransfer_Click(object sender, EventArgs e)
        {
            string name = gleOrderRef.EditValue.ToStr();
            DateTime date = ExpireDate.Value;
            brandId = gleBrand.EditValue.ToInt();
            sezonId = gleSezon.EditValue.ToInt();
            string notlar = txtNot.Text;

            

            if (name.IsNullOrWhiteSpace() && !chkIdle.Checked)
            {
                ShowErrorMessage("Lütfen rezerve ismi girin !");
                return;
            }
            if (brandId==0)
            {
                ShowErrorMessage("Lütfen marka seçiniz !");
                return;
            }
            if (sezonId==0)
            {
                ShowErrorMessage("Lütfen sezon seçiniz !");
                return;
            }
            if (date<=DateTime.Today)
            {
                ShowErrorMessage("Rezerve bitiş tarihi bugünden küçük veya eşit olamaz!");
                return;
            }
            
            rezerveId = ExecuteScalar("InsAksesuarRezerve", Aksesuar, name, brandId, sezonId, date, notlar, WhoAmI,KaufIdx).ToInt(-1);
            this.DialogResult = DialogResult.OK;
        }
    }
}